package com.qianbao.qianbaobusiness.ui.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextWatcher;
import android.widget.TextView;

import com.androidbase.dialog.LoadingDialog;
import com.androidbase.utils.RegexUtils;
import com.androidbase.utils.RxUtils;
import com.androidbase.utils.ToastUtils;
import com.baseandroid.config.Global;
import com.baseandroid.mvp.BaseMvpActivity;
import com.baseandroid.retrofit.RxObserver;
import com.baseandroid.utils.FormatUtil;
import com.jaeger.library.StatusBarUtil;
import com.qianbao.qianbaobusiness.R;
import com.qianbao.qianbaobusiness.ui.home.HomeActivity;
import com.qianbao.qianbaobusiness.ui.widget.EditTextWithDel;

import java.util.ArrayList;

import butterknife.BindView;
import timber.log.Timber;

public class LoginActivity extends BaseMvpActivity<LoginView, LoginPresent> implements LoginView {
    private static Boolean tryYajin = false;
    @BindView(R.id.login_phonenumber)
    EditTextWithDel login_phonenumber;
    @BindView(R.id.login_count)
    TextView        login_count;
    @BindView(R.id.login_verificationcode)
    EditTextWithDel login_verificationcode;
    @BindView(R.id.login)
    TextView        login;

    /*--白名单--*/
    private ArrayList<String> mPhonNumbers;

    LoadingDialog mLoadingDialog;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login_layout;
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void setupView() {
        StatusBarUtil.setTranslucentForImageViewInFragment(LoginActivity.this, 0, null);
        mPhonNumbers = new ArrayList<>();//nhl 上线版本只添加一个
        mPhonNumbers.add("18964680703");//18964680703/123456
        mPhonNumbers.add("13888888888");//13888888888/6688
        //禁止输入框输入空格和换行符号
        InputFilter filter = new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                if (source.equals(" ") || source.toString().contentEquals("\n")) {
                    return "";
                } else {
                    return null;
                }
            }
        };
        login_phonenumber.setFilters(new InputFilter[]{filter, new InputFilter.LengthFilter(13)});

        login_phonenumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                checkEditTextGet(s.toString());
                FormatUtil.addEditSpace(s, start, before, login_phonenumber);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        login_verificationcode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                checkEditTextLogin(s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        login_phonenumber.setText(Global.getUsrePhotoNumber());

        RxUtils.clickDebounceStream(login_count)
                .compose(RxUtils.<Integer>applySchedulersLifeCycle(getMvpView()))
                .subscribe(new RxObserver<Integer>() {
                    @Override
                    public void onNext(Integer integer) {
                        Timber.e("xxx" + integer);
                        if (RegexUtils.isMobileSimple(login_phonenumber.getText()
                                                              .toString()
                                                              .replaceAll(" ", ""))) {
                            getPresenter().requestSmsCode(login_phonenumber.getText()
                                                                  .toString()
                                                                  .replaceAll(" ", ""));
                        } else {
                            ToastUtils.showShortToastSafe(R.string.login_input_mobile_tip);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                });

        RxUtils.clickDebounceStream(login)
                .compose(RxUtils.<Integer>applySchedulersLifeCycle(getMvpView()))
                .subscribe(new RxObserver<Integer>() {
                    @Override
                    public void onNext(Integer integer) {
                        getPresenter().requestLogin(login_phonenumber.getText()
                                                            .toString()
                                                            .replaceAll(" ", ""), login_verificationcode.getText()
                                                                                          .toString(), mPhonNumbers);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                });
    }

    @Override
    protected void setupData(Bundle savedInstanceState) {

    }

    public void checkEditTextGet(String content) {
        String s = content.replaceAll(" ", "");
        if (!RegexUtils.isMobileSimple(s)) {
            return;
        }

        if (login_count.getText().equals(getString(R.string.login_verfycode))) {
            login_count.setEnabled(s.length() > 10 ? true : false);
        }
    }

    public void checkEditTextLogin(String content) {
        login.setEnabled(content.length() > 3 ? true : false);
    }

    @Override
    public void disableValidateCodeButton() {
        login_count.setEnabled(false);
    }

    @Override
    public void enableValidateCodeButton() {
        login_count.setEnabled(true);
    }

    @Override
    public void countValidateCodeButton(long number) {
        login_count.setText("已发送（" + number + ")");
    }

    @Override
    public void resendValidateCodeButton() {
        login_count.setEnabled(true);
        login_count.setText("重发验证码");
    }

    @Override
    public void showLoadingDialog() {
        if (mLoadingDialog == null) {
            mLoadingDialog = new LoadingDialog();
        }

        if (!mLoadingDialog.isVisible()) {
            mLoadingDialog.show(getSupportFragmentManager(), "loadingdialog");
        }
    }

    @Override
    public void closeLoadingDialog() {
        mLoadingDialog.dismissAllowingStateLoss();
    }


    @Override
    public void OnLoginInfo(boolean state) {
        if (state) {
            ToastUtils.showShortToast("登录成功");
            Global.setLoginCurrently(true);
            startActivity(new Intent(this, HomeActivity.class));
            finish();
        }
    }

}
